﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for Photo
    /// </summary>
    public class Photo
    {
        private int _PhotoID;
        private int _AlbumID;
        private String _PhotoCaption;
        private DateTime _PhotoCreateDate;
        private bool _IsShown;
        private string _FileName;

        public string FileName
        {
            get
            {
                return _FileName;
            }
            set
            {
                _FileName = value;
            }
        }

        public int PhotoID
        {
            get
            {
                return _PhotoID;
            }
            set
            {
                _PhotoID = value;
            }
        }

        public int AlbumID
        {
            get
            {
                return _AlbumID;
            }
            set
            {
                _AlbumID = value;
            }
        }

        public String PhotoCaption
        {
            get
            {
                return _PhotoCaption;
            }
            set
            {
                _PhotoCaption = value;
            }
        }

        public DateTime PhotoCreateDate
        {
            get
            {
                return _PhotoCreateDate;
            }
            set
            {
                _PhotoCreateDate = value;
            }
        }

        public bool IsShown
        {
            get
            {
                return _IsShown;
            }
            set
            {
                _IsShown = value;
            }
        }


        public Photo(int AlbumID)
        {
            //
            // TODO: Add constructor logic here
            //
            _PhotoID = -1;
            _IsShown = false;
            _FileName = "";
            _PhotoCreateDate = DateTime.Now;
            _PhotoCaption = "";
            _AlbumID = AlbumID;
        }

        public Photo(SqlDataReader SqlReader)
        {
            if (SqlReader != null && !SqlReader.IsClosed)
            {
                _PhotoID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    _AlbumID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    _FileName = SqlReader.GetString(2);

                }
                if (!SqlReader.IsDBNull(3))
                {
                    _PhotoCaption = SqlReader.GetString(3);

                }
                if (!SqlReader.IsDBNull(4))
                {
                    _PhotoCreateDate = SqlReader.GetDateTime(4);

                }
                if (!SqlReader.IsDBNull(5))
                {
                    _IsShown = SqlReader.GetBoolean(5);
                }
                if (!SqlReader.IsDBNull(6))
                {
                    _FileName = SqlReader.GetString(6);
                }
            }
        }

        public static DataTable GetAll()
        {
            DBHelp Dbh = new DBHelp();
            DataTable dt = new DataTable();
            String cmd = "Photo_GetAll";
            dt = Dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static DataTable GetByAlbum(int AlbumID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@album_id", AlbumID);
            DataTable dt = new DataTable();
            String cmd = "select * from tbl_photo where album_id = @album_id";
            dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            return dt;
        }

        public static Photo GetInfo(int PhotoID)
        {
            Photo photo = new Photo(-1);
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@photo_id", PhotoID);
            string cmd = "Photo_GetInfo";
            SqlDataReader SqlReader = Dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (SqlReader.Read())
            {
                photo.PhotoID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    photo.AlbumID = SqlReader.GetInt32(1);
                }
                
                if (!SqlReader.IsDBNull(2))
                {
                    photo.PhotoCaption = SqlReader.GetString(2);

                }
                if (!SqlReader.IsDBNull(3))
                {
                    photo.PhotoCreateDate = SqlReader.GetDateTime(3);

                }
                if (!SqlReader.IsDBNull(4))
                {
                    photo.IsShown = SqlReader.GetBoolean(4);
                }
                if (!SqlReader.IsDBNull(5))
                {
                    photo.FileName = SqlReader.GetString(5);
                }
            }
            return photo;
        }

        public static int Insert(Photo photo)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@album_id", photo.AlbumID);
            Dbh.AddParameter("@caption", photo.PhotoCaption);
            Dbh.AddParameter("@created_date", photo.PhotoCreateDate);
            Dbh.AddParameter("@is_shown", photo.IsShown);
            Dbh.AddParameter("@name", photo.FileName);
            SqlParameter PhotoID = new SqlParameter();
            PhotoID.DbType = DbType.Int32;
            PhotoID.Direction = ParameterDirection.Output;
            PhotoID.ParameterName = @"@photo_id";
            Dbh.AddParameter(PhotoID);

            string cmd = "Photo_Insert";
            try
            {
                Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
                return (int)PhotoID.Value;
            }
            catch (System.Exception)
            {
                return -1;
            }

        }

        public static void Update(Photo photo)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@photo_id", photo.PhotoID);
            Dbh.AddParameter("@album_id", photo.AlbumID);
            Dbh.AddParameter("@caption", photo.PhotoCaption);
            Dbh.AddParameter("@created_date", photo.PhotoCreateDate);
            Dbh.AddParameter("@is_shown", photo.IsShown);
            Dbh.AddParameter("@name", photo.FileName);

            string cmd = "Photo_Update";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        public static void Delete(int PhotoID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@photo_id", PhotoID);
            string cmd = "Photo_Delete";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    }
}